package com.sy.struts;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.sy.model.DocAffix;
import com.sy.service.AffixService;

/**
 * 
 * @author user
 *
 */
@SuppressWarnings("serial")
public class AffixAction extends ActionSupport {
	private static final int BUFFER_SIZE = 16 * 1024 ;
	private String fileDescriptor;
	String fileNameType;	
    private File uploadFile;  				//封装上传文件域的属性
    private String uploadFileContentType;   	//封装上传文件类型的属性
    private String uploadFileFileName; 
    private String savePath;   				//接受依赖注入的属性
    private File[] uploadFiles;  				//用File数组来封装多个上传文件域对象
	private String[] uploadFilesFileName;  	//用String数组来封装多个上传文件名
	private String[] uploadFilesContentType;  	//用String数组来封装多个上传文件类型
	private String saveToFileName;
	private List<String> saveToFileNames;
	
    public void setSavePath(String value) {  	//接受依赖注入的方法  
        this.savePath = value;
    }
    
    @SuppressWarnings("deprecation")
	private String getSavePath(){
        return ServletActionContext.getRequest().getRealPath(savePath);
    }   
	public AffixService affixService;
	private Long docAffixId;
	private String downFilePathFileName;
	public void setDownFilePathFileName(String downFilePathFileName) {
		this.downFilePathFileName = downFilePathFileName;
	}
	public InputStream getInputStream() throws Exception {
		return ServletActionContext.getServletContext().getResourceAsStream(downFilePathFileName);
	}
	public String removeAffix(){
		affixService.deleteByAffixId(docAffixId);
		return null;
	}
	public String downLoadAffix() {
		HttpServletResponse response = ServletActionContext.getResponse();
		String realPath = ServletActionContext.getServletContext().getRealPath("");
		DocAffix da;
		FileInputStream fis =null;
		OutputStream fout=null;
		try {
			da =affixService.getAffixWithContentById(docAffixId);
			response.setHeader("Content-Disposition", "attachment;filename=\""
					+ URLEncoder.encode(da.getDocAffixName(), "UTF-8")
					+ "\"");
			response.setContentType(da.getDocAffixType());
			fis = new FileInputStream(realPath + da.getDocAffixPath());
			fout = response.getOutputStream();
	        int byteRead = 0;
	        final int length = 8192;
	        byte[] buffer = new byte[length];
	 		while ((byteRead = fis.read(buffer)) != -1) {
		 		fout.write(buffer, 0, byteRead);
		 	}
	 		if(fout!=null){
				fout.close();
			}
	 		if(fis!=null){
	 			fis.close();
	 		}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			fout=null;
			fis=null;
		}		
		return null;
	}
	
	public String upLoadProductImageAction(){
		//以服务器的文件保存地址和原文件名建立上传文件输出流
    	saveToFileName=null;    	
    	if(fileNameType.equals("1")){   //采用原始的文件名称 
    		saveToFileName=getSavePath()+ "\\" + getUploadFileFileName(); 		
    	}
    	else{ //文件名由系统时间与上传文件的后缀组成
    		saveToFileName=getSavePath()+"/"+(new Date().getTime()+getExtention(uploadFileFileName));
    	}
    	doFileUpload(getUploadFile(),new File(saveToFileName));  
        return SUCCESS;
	}    
	private static String getExtention(String fileName) {
    	int pos = fileName.lastIndexOf(".");
    	return fileName.substring(pos);
    } 
	private void doFileUpload(File src, File dst) {
    	try {
    		InputStream in = null ;
    		OutputStream out = null ;
    		try {                
    			in = new BufferedInputStream(new FileInputStream(src), BUFFER_SIZE);
    			out = new BufferedOutputStream(new FileOutputStream(dst), BUFFER_SIZE);
    			byte [] buffer = new byte [BUFFER_SIZE];
    			while (in.read(buffer) > 0 ) {
    				out.write(buffer);    //将上传文件的内容写入服务器
    			} 
    		} finally {
    			if ( null != in) {
    				in.close();
    			} 
    			if ( null != out) {
    				out.close();
    			} 
    		} 
    	} catch (Exception e) {
    		e.printStackTrace();
    	} 
    }
	public String multiFileUploadAction(){
		File[] srcFiles = this.getUploadFiles();   //获得每个上传的文件对象
		saveToFileNames=new ArrayList<String>();
		for (int index = 0; index <srcFiles.length; index++) {
			UUID uuid=UUID.randomUUID();
			String dstPath = getSavePath()+ "\\" +uuid+getExtention(uploadFilesFileName[index]) ;		
			File dstFile = new File(dstPath);
			saveToFileNames.add(dstPath);
			this.doFileUpload (srcFiles[index], dstFile);
		}
		return SUCCESS;
	}
	public AffixService getAffixService() {
		return affixService;
	}
	public void setAffixService(AffixService affixService) {
		this.affixService = affixService;
	}
	public Long getDocAffixId() {
		return docAffixId;
	}
	public void setDocAffixId(Long docAffixId) {
		this.docAffixId = docAffixId;
	}
	public String getFileDescriptor() {
		return fileDescriptor;
	}
	public void setFileDescriptor(String fileDescriptor) {
		this.fileDescriptor = fileDescriptor;
	}
	public String getFileNameType() {
		return fileNameType;
	}
	public void setFileNameType(String fileNameType) {
		this.fileNameType = fileNameType;
	}
	public File getUploadFile() {
		return uploadFile;
	}
	public void setUploadFile(File uploadFile) {
		this.uploadFile = uploadFile;
	}
	public String getUploadFileContentType() {
		return uploadFileContentType;
	}
	public void setUploadFileContentType(String uploadFileContentType) {
		this.uploadFileContentType = uploadFileContentType;
	}
	public String getUploadFileFileName() {
		return uploadFileFileName;
	}
	public void setUploadFileFileName(String uploadFileFileName) {
		this.uploadFileFileName = uploadFileFileName;
	}
	public String getDownFilePathFileName() {
		return downFilePathFileName;
	}
	public File[] getUploadFiles() {
		return uploadFiles;
	}
	public void setUploadFiles(File[] uploadFiles) {
		this.uploadFiles = uploadFiles;
	}
	public String[] getUploadFilesFileName() {
		return uploadFilesFileName;
	}
	public void setUploadFilesFileName(String[] uploadFilesFileName) {
		this.uploadFilesFileName = uploadFilesFileName;
	}
	public String[] getUploadFilesContentType() {
		return uploadFilesContentType;
	}
	public void setUploadFilesContentType(String[] uploadFilesContentType) {
		this.uploadFilesContentType = uploadFilesContentType;
	}
	public String getSaveToFileName() {
		return saveToFileName;
	}
	public void setSaveToFileName(String saveToFileName) {
		this.saveToFileName = saveToFileName;
	}
	public List<String> getSaveToFileNames() {
		return saveToFileNames;
	}
	public void setSaveToFileNames(List<String> saveToFileNames) {
		this.saveToFileNames = saveToFileNames;
	}
	
}
